import * as vscode from 'vscode';
import { VerticalDiffManager } from '../../../src/diff/vertical/manager';
import { LLMFactory } from '../../../core/llm/llmFactory';
import { BaseLLM } from "../../../core/llm/baseLLM";
import { ChatMessage } from '../../../core';

export class FixCodeHandler {
    private _verticalDiffManager: VerticalDiffManager;
    private _llmFactory: LLMFactory;

    constructor(verticalDiffManager: VerticalDiffManager, llmFactory: LLMFactory) {
        this._verticalDiffManager = verticalDiffManager;
        this._llmFactory = llmFactory;
    }

    public async fixCode(prompt: ChatMessage[]) {
        const llm: BaseLLM | undefined = this._llmFactory.getLLM("deepseek-chat");
        if (!llm) {
            vscode.window.showErrorMessage("LLM not found");
            return;
        }
        await this._verticalDiffManager.streamEdit(llm._streamChat(prompt, new AbortController().signal), this.getFullFileRange());
    }

    private getRange(startLine0: number, endLine0: number): vscode.Range {
        const startLine = Math.min(startLine0, endLine0);
        const endLine = Math.max(startLine0, endLine0);
        return new vscode.Range(startLine, 0, endLine, Number.MAX_SAFE_INTEGER);
    }
    private getFullFileRange(): vscode.Range {
        const document = vscode.window.activeTextEditor?.document;
        if (!document) {
            throw new Error("No active text editor found");
        }
        const lastLine = document.lineCount - 1;
        return new vscode.Range(0, 0, lastLine, Number.MAX_SAFE_INTEGER);
    }
}